package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.business.entity.Card;
import cn.stylefeng.guns.modular.business.mapper.CardMapper;
import cn.stylefeng.guns.modular.business.model.dto.KeyValueDTO;
import cn.stylefeng.guns.modular.business.model.params.CardParam;
import cn.stylefeng.guns.modular.business.model.result.CardResult;
import cn.stylefeng.guns.modular.business.model.result.UserPropertyResult;
import  cn.stylefeng.guns.modular.business.service.CardService;
import cn.stylefeng.guns.sys.core.util.DateUtil;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 卡号 服务实现类
 * </p>
 *
 * @author jwj
 * @since 2019-07-12
 */
@Service
public class CardServiceImpl extends ServiceImpl<CardMapper, Card> implements CardService {

    @Override
    public void add(CardParam param){
        Card entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(CardParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(CardParam param){
        Card oldEntity = getOldEntity(param);
        Card newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public CardResult findBySpec(CardParam param){
        return null;
    }

    @Override
    public List<CardResult> findListBySpec(CardParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(CardParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    /**
     * 查询卡明细
     *
     * @param cardId
     * @return
     */
    @Override
    public CardResult findById(String cardId) {
        Card card = getById(cardId);
        CardResult result = new CardResult();
        ToolUtil.copyProperties(card, result);
        result.setCreateTimeStr(DateUtil.convert(result.getCreateTime()));
        return result;
    }

    private Serializable getKey(CardParam param){
        return param.getCardId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private Card getOldEntity(CardParam param) {
        return this.getById(getKey(param));
    }

    private Card getEntity(CardParam param) {
        Card entity = new Card();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }


    /**
     * 获取用户总面值
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public Integer totalQuota(Long userId) {
        return this.baseMapper.totalQuota(userId);
    }

    /**
     * 获取所有用户总面值
     *
     * @return
     */
    @Override
    public UserPropertyResult allUserQuota(Long userId) {
        return this.baseMapper.allUserQuota(userId);
    }

}
